﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using LightWeb.Blog.Common;
using System.Threading;
using LightWeb.Respository;
using LightWeb.DomainModel;
using LightWeb.TransferModel;

namespace LightWeb.Blog.DataService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ArticalService" in code, svc and config file together.
    public class ArticalService : IArticalService
    {
        public TmResult NewComment(TransferModel.TmArticalComment comment)
        {
            //var obj = Utility.GetJSONFromObject(new TransferModel.TmArticalComment
            //{
            //    Name = "Flyear",
            //    Content = "Flyear's comment",
            //    Date = DateTime.Now.ToString(),
            //    ArticalId = 1
            //});
            //obj = Utility.GetJSONFromObject(comment);
            //Thread.Sleep(5000);
            //return obj;
            try
            {
                using (OurTransactionScope ts = new OurTransactionScope())
                {
                    var context = DataContext.Current;
                    context.Session.Save(new ArticalComment()
                    {
                        ArticalId = comment.ArticalId,
                        Content = comment.Content,
                        Date = DateTime.Parse(comment.Date),
                        Email = comment.Email,
                        Name = comment.Name,
                        Site = comment.Site
                    });
                    ts.Complete();
                }
                return new TmResult();
            }
            catch (Exception ex)
            {
                return new TmResult(ex.Message);
            }
        }
    }
}
